import 'package:flutter/material.dart';

class TestFutureBuild extends StatefulWidget {
  @override
  _TestFutureBuildState createState() => _TestFutureBuildState();
}

class _TestFutureBuildState extends State<TestFutureBuild> {
  var _futureWork;

  @override
  void initState() {
    _futureWork = _initBeforeBuild();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("test future build"),
      ),
      body: FutureBuilder(
        future: _futureWork,
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
              break;
            case ConnectionState.waiting:
              return Container(
                child: Text("waiting, loading"),
              );
              break;
            case ConnectionState.active:
              break;
            case ConnectionState.done:
              return Container(
                child: Text("done"),
              );
              break;
          }
          return Container(
            child: Text("others"),
          );
        },
      ),
    );
  }

  Future _initBeforeBuild() async {
    // 这里做必要的初始化操作
    await Future.delayed(Duration(milliseconds: 3000));
    print("init finish");
    return;
  }
}
